﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EnviamentDeDocuments.Formularis
{
    public partial class frmProces : Form
    {

        public enum EstatEnviament
        { 
            Enviant,
            Aturat,
            Acabat
        }

        public int ValorMaximBarraProgres
        {
            get { return this.ProgressBar.Maximum; }
            set { this.ProgressBar.Maximum = value; }
        }

        delegate void AvancaBarraProgresDelegate();
        public void AvancaBarraProgres()
        {
            if (this.ProgressBar.InvokeRequired)
            {
                AvancaBarraProgresDelegate d = new AvancaBarraProgresDelegate(AvancaBarraProgres);
                this.Invoke(d, new object[] {  });
            }
            else
            {
                this.ProgressBar.PerformStep();
            }           
        }

        delegate void setInfoAccioActualDelegate(string info);
        public void setInfoAccioActual(string info)
        {
            if (this.txtOperacioActual.InvokeRequired)
            {
                setInfoAccioActualDelegate d = new setInfoAccioActualDelegate(setInfoAccioActual);
                this.Invoke(d, new object[] { info });
            }
            else
            {
                this.txtOperacioActual.Text = info;
            }
        }

        delegate void addGridRowDelegate(string client, int numDocuments, string observacions);
        public void addGridRow(string client, int numDocuments, string observacions)
        {
            if (this.dataGridView.InvokeRequired)
            {
                addGridRowDelegate d = new addGridRowDelegate(addGridRow);
                this.Invoke(d, new object[] { client,numDocuments,observacions });
            }
            else
            {
                this.dataGridView.Rows.Insert(0, new object[] { client, numDocuments, observacions });
            }
        }

        private EstatEnviament _Estat = EstatEnviament.Enviant;
        public EstatEnviament Estat 
        {
            get { return _Estat; }
            set
            {
                _Estat = value;
                ActualitzaEstatBotons();
            }
        }

        delegate void ActualitzaEstatBotonsDelegate();
        public void ActualitzaEstatBotons()
        {
            if (this.cmdCancelarContinuar.InvokeRequired)
            {
                ActualitzaEstatBotonsDelegate d = new ActualitzaEstatBotonsDelegate(ActualitzaEstatBotons);
                this.Invoke(d, new object[] {  });
            }
            else
            {
                switch (Estat)
                {
                    case EstatEnviament.Acabat:
                        this.cmdCancelarContinuar.Visible = false;
                        this.cmdEnrere.Enabled = true;
                        this.cmdTancar.Enabled = true;
                        break;
                    case EstatEnviament.Aturat:
                        this.cmdCancelarContinuar.Text = "Continuar";
                        this.cmdCancelarContinuar.Visible = true;
                        this.cmdEnrere.Enabled = true;
                        this.cmdTancar.Enabled = true;
                        break;
                    case EstatEnviament.Enviant:
                        this.cmdCancelarContinuar.Text = "Cancelar";
                        this.cmdCancelarContinuar.Visible = true;
                        this.cmdEnrere.Enabled = false;
                        this.cmdTancar.Enabled = false;
                        break;
                }

            }
        }

        public bool Continuar { get; private set; }
        public bool CancelContinuePressed { get; set; }

        public frmProces()
        {
            InitializeComponent();
            ProgressBar.Step = 1;
            CancelContinuePressed = false;
            Continuar = false;
        }

        private void cmdTancar_Click(object sender, EventArgs e)
        {
            Continuar = true;
            this.Close();
        }

        private void cmdEnrere_Click(object sender, EventArgs e)
        {
            Continuar = false;
            this.Close();
        }

        private void cmdCancelar_Click(object sender, EventArgs e)
        {
            CancelContinuePressed = true;
        }

    }
}
